package com.platform.controller.web;

import com.alibaba.dashscope.utils.JsonUtils;
import com.platform.common.annotation.Log;
import com.platform.common.constant.Constants;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.SecurityUtils;
import com.platform.common.utils.StringUtils;
import com.platform.system.domain.dto.device.ChatBackgroundMusicDto;
import com.platform.system.domain.dto.device.HumanModelManagerDto;
import com.platform.system.domain.dto.device.SpeakerInfoDto;
import com.platform.system.domain.vo.device.HumanModelManagerVo;
import com.platform.system.service.ISysChatBackgroundMusicService;
import com.platform.system.service.ISysHumanModelManagerService;
import com.platform.system.service.ISysSpeakerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Huangkai
 */
@Slf4j
@RestController
@RequestMapping("/humanModel/web")
public class WebHumanModelManagerController {

    @Resource
    ISysHumanModelManagerService humanModelManagerService;

    @Autowired
    ISysSpeakerInfoService sysSpeakerInfoService;

    @Resource
    ISysChatBackgroundMusicService sysChatBackgroundMusicService;

    /**
     * 通过类型查询人设
     */
    @Log(title = "查询人设配置信息", businessType = BusinessType.OTHER)
    @PostMapping("/selectHumanModelManagerList")
    public TableDataInfo selectHumanModelManagerList(@RequestBody @Validated HumanModelManagerVo humanModelManagerVo) {
        log.info("客户端获取人设{}", JsonUtils.toJson(humanModelManagerVo));
        humanModelManagerVo.setStatus(Constants.NUM_ZERO);
        return humanModelManagerService.selectHumanModelManagerList(humanModelManagerVo);
    }
    /**
     * 插入人设客户端
     */
    @Log(title = "插入人设客户端", businessType = BusinessType.INSERT)
    @PostMapping("/insertHumanModelManager")
    public AjaxResult insertHumanModelManager(@RequestBody @Validated HumanModelManagerVo humanModelManagerVo) {
        humanModelManagerVo.setUserUid(SecurityUtils.getWebLoginUser().getUid());
        return humanModelManagerService.insertHumanModelManager(humanModelManagerVo);
    }


    /**
     * 查询详情
     */
    @Log(title = "查询详情", businessType = BusinessType.OTHER)
    @GetMapping("/selectHumanModelManager/{uid}")
    public AjaxResult selectHumanModelManager(@PathVariable(value = "uid") String uid) {
        HumanModelManagerDto humanModelManagerDto = humanModelManagerService.selectHumanModelManager(uid);
        if(humanModelManagerDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        if(StringUtils.isNotEmpty(humanModelManagerDto.getSpeakerUid())){
            SpeakerInfoDto speakerInfoDto = sysSpeakerInfoService.selectSpeakerInfo(humanModelManagerDto.getSpeakerUid());
            humanModelManagerDto.setSpeakerInfoDto(speakerInfoDto);
        }
        if(StringUtils.isNotEmpty(humanModelManagerDto.getChatBackgroundMusicUid())){
            ChatBackgroundMusicDto chatBackgroundMusicDto = sysChatBackgroundMusicService.selectChatBackgroundMusic(humanModelManagerDto.getChatBackgroundMusicUid());
            humanModelManagerDto.setChatBackgroundMusicDto(chatBackgroundMusicDto);
        }
        return AjaxResult.success(humanModelManagerDto);
    }
}
